<?php
class Index extends Action {
	private $limit = 10;
	private $status = array ('new','pass','unpass','spam','deleted' );
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$status = rqst ( 'status', 'new' );
		$status = in_array ( $status, $this->status ) ? $status : 'new';
		$data ['status'] = $status;
		if ($status == 'deleted') {
			$where ['deleted'] = 1;
		} else {
			$where ['deleted'] = 0;
			$where ['status'] = $status;
		}
		$page_id = irqst ( 'page_id' );
		if (! empty ( $page_id )) {
			$where ['WP.page_id'] = $page_id;
			$data ['page_id'] = $page_id;
		}
		$key = rqst ( 'key' );
		if (! empty ( $key )) {
			$like ['comment LIKE'] = "%{$key}%";
			$like ['OR'] = array ('WP.page_title LIKE' => "%{$key}%" );
			$like ['OR'] = array ('author LIKE' => "%{$key}%" );
			$like ['OR'] = array ('subject LIKE' => "%{$key}%" );
			$where ['AND'] = $like;
			$data ['key'] = $key;
		}
		$start = rqst ( 'start', 1 ); // 分页
		$cmtM = new WebCommentModel ();
		
		$cmtM->alias ( 'CMT' )->join ( 'web_page AS WP', "CMT.page_id = WP.page_id" )->count ( true )->sort ( array ('create_time','d' ) );
		$items = $cmtM->where ( $where )->limit ( $this->limit, $start - 1 )->retrieve ( 'CMT.*,WP.url AS page_url,WP.title AS page_title' );
		
		if ($items) {
			$data ['items'] = $items;
			$data ['totalCount'] = $items->countTotal;
		} else {
			$data ['items'] = array ();
			$data ['totalCount'] = 0;
		}
		$data ['limit'] = $this->limit;
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		$data ['navi'] = 'comment';
		$data ['page_tip_info'] = sess_del ( 'page_tip_info' );
		
		$data ['new_total'] = $cmtM->where ( array ('deleted' => 0,'status' => 'new' ) )->count ();
		$data ['pass_total'] = $cmtM->where ( array ('deleted' => 0,'status' => 'pass' ) )->count ();
		$data ['unpass_total'] = $cmtM->where ( array ('deleted' => 0,'status' => 'unpass' ) )->count ();
		$data ['spam_total'] = $cmtM->where ( array ('deleted' => 0,'status' => 'spam' ) )->count ();
		$data ['deleted_total'] = $cmtM->where ( array ('deleted' => 1 ) )->count ();
		
		sess_add ( 'comment_list_url', $this->url );
		bind ( 'get_comment_operations', array ($this,'get_comment_operations' ), 1, 2 );
		bind ( 'get_comment_bench_options', array ($this,'get_comment_bench_options' ), 1, 2 );
		return new SmartyView ( $data, 'comment/Index' );
	}
	public function get_comment_operations($opts, $item) {
		if ($item ['deleted']) {
			$opts .= '<a href="./?Ctlr=SetStatus&del=0&cid=' . $item ['comment_id'] . '" class="reset-cmt tgre"><i class="icon-share-alt"></i>还原</a>';
			$opts .= '<a onclick="return confirm(\'你确定要删除该评论?\')" href="./?Ctlr=DelComment&cid=' . $item ['comment_id'] . '" class="del-cmt tred"><i class="icon-remove-sign"></i>永久删除</a>';
		} else {
			if ($item ['status'] == 'new' || $item ['status'] == 'unpass') {
				$opts .= '<a href="./?Ctlr=SetStatus&s=pass&cid=' . $item ['comment_id'] . '" class="pass-cmt tgre"><i class="icon-thumbs-up"></i>批准</a>';
			}
			if ($item ['status'] == 'new' || $item ['status'] == 'pass') {
				$opts .= '<a href="./?Ctlr=SetStatus&s=unpass&cid=' . $item ['comment_id'] . '" class="unpass-cmt torg"><i class="icon-thumbs-down"></i>驳回</a>';
				$opts .= '<a href="./?Ctlr=EditComment&cid=' . $item ['comment_id'] . '" class="edit-cmt"><i class="icon-edit"></i>编辑</a>';
				$opts .= '<a href="./comment/?Ctlr=ReplyComment&cid=' . $item ['comment_id'] . '" class="reply-cmt"><i class="icon-comment"></i>回复</a>';
			}
			if ($item ['status'] != 'spam') {
				$opts .= '<a href="./?Ctlr=SetStatus&s=spam&cid=' . $item ['comment_id'] . '" class="spam-cmt torg"><i class="icon-volume-off"></i>垃圾评论</a>';
				$opts .= '<a href="./?Ctlr=SetStatus&del=1&cid=' . $item ['comment_id'] . '" class="trash-cmt tred"><i class="icon-trash"></i>移至回收站</a>';
			}
			if ($item ['status'] == 'spam') {
				$opts .= '<a href="./?Ctlr=SetStatus&s=new&cid=' . $item ['comment_id'] . '" class="new-cmt tgre"><i class="icon-volume-up"></i>这不是垃圾评论</a>';
				$opts .= '<a onclick="return confirm(\'你确定要删除该评论?\')" href="./?Ctlr=DelComment&cid=' . $item ['comment_id'] . '" class="del-cmt tred"><i class="icon-remove-sign"></i>永久删除</a>';
			}
		}
		return $opts;
	}
	public function get_comment_bench_options($opts, $status) {
		if ($status == 'deleted') {
			$opts .= '<li><a href="./?Ctlr=SetStatus&del=0" class="menu-reset-cmt tgre"><i class="icon-share-alt"></i>还原</a></li>';
			$opts .= '<li><a href="./?Ctlr=DelComment" class="menu-del-cmt tred"><i class="icon-remove-sign"></i>永久删除</a></li>';
		} else if ($status == 'spam') {
			$opts .= '<li><a href="./?Ctlr=SetStatus&s=new" class="menu-new-cmt tgre"><i class="icon-volume-up"></i>这不是垃圾评论</a></li>';
			$opts .= '<li><a href="./?Ctlr=DelComment" class="menu-del-cmt tred"><i class="icon-remove-sign"></i>永久删除</a></li>';
		} else {
			if ($status == 'new' || $status == 'unpass') {
				$opts .= '<li><a href="./?Ctlr=SetStatus&s=pass" class="menu-pass-cmt tgre"><i class="icon-thumbs-up"></i>批准</a></li>';
			}
			if ($status == 'new' || $status == 'pass') {
				$opts .= '<li><a href="./?Ctlr=SetStatus&s=unpass" class="menu-unpass-cmt torg"><i class="icon-thumbs-down"></i>驳回</a></li>';
			}
			$opts .= '<li><a href="./?Ctlr=SetStatus&s=spam" class="menu-spam-cmt torg"><i class="icon-volume-off"></i>垃圾评论</a></li>';
			$opts .= '<li><a href="./?Ctlr=SetStatus&del=1" class="menu-trash-cmt tred"><i class="icon-trash"></i>移至回收站</a></li>';
		}
		return $opts;
	}
}